import { Collapse, Tree } from 'antd';
import React, { useEffect, useState } from 'react';
import { LayoutB } from '~/public/cpas-ui';
// import Accordion, { AccordionTypes } from 'devextreme-react/accordion';
import { msgInfo } from '@/utils/msg';
import type { DataNode } from 'antd/es/tree';
import { useModel } from 'umi';
import PageLoading from '../components/PageLoading';
import './index.less';
import RightTables from './rightTables';
import { getAccountInfo } from './service';
import { useElementResizeWidth } from './hooks/useElementResizeWidth';

const { Panel } = Collapse;

// 创建一个 Context 对象
const TreeSelectKeyContext = React.createContext({});

const AccountingEntryTest = () => {
  const resizeWidthObj = useElementResizeWidth(document.querySelector('#entryTest-rightContainer')!)
  const { initialState } = useModel('@@initialState');
  const { dbname, sjnd } = initialState?.curPart as any;

  const [treeSelecNode, setTreeSelecNode] = useState<any>({
    treeSelectKey: '0-0-0',
    treeSelectName: '',
    tableYearArr: [],
    spinLoad: true,
    yearID: sjnd,
  });

  const [selectedItems1, setSelectedItems1] = useState(['0-0-0']);
  const [selectedItems2, setSelectedItems2] = useState(['']);

  const getData = async () => {
    const res: any = await getAccountInfo({
      dbname,
    });
    if (res.success) {
      setTreeSelecNode((oldData: any) => {
        const newData: any = { ...oldData };
        newData.tableYearArr = res.data;
        return newData;
      });
    }
  };

  useEffect(() => {
    getData();
  }, []);

  // 完整性测试
  const integrityData: DataNode[] = [
    {
      title: '借方发生额与贷方发生额是否一致',
      key: '0-0-0',
      // icon: ({ selected }) => (selected ? <FrownFilled /> : <FrownOutlined />),
    },
    {
      title: '跳号测试',
      key: '0-0-1',
      // icon: ({ selected }) => (selected ? <FrownFilled /> : <FrownOutlined />),
    },
  ];
  // 细节测试
  const detailData: DataNode[] = [
    {
      title: '会计人员清单及会计分录作出人员测试',
      key: '0-0-0-0',
      // icon: ({ selected }) => (selected ? <FrownFilled /> : <FrownOutlined />),
    },
    {
      title: '分录涉及不相关、异常或很少使用的账户',
      key: '0-0-0-1',
      // icon: ({ selected }) => (selected ? <FrownFilled /> : <FrownOutlined />),
    },
    {
      title: '分录在期末或结账过程中作出，且没有或只有很少的解释或描述',
      key: '0-0-0-2',
      // icon: ({ selected }) => (selected ? <FrownFilled /> : <FrownOutlined />),
    },
    {
      title: '分录在编制财务报表之前或编制过程中作出且没有科目代码',
      key: '0-0-0-3',
      // icon: ({ selected }) => (selected ? <FrownFilled /> : <FrownOutlined />),
    },
    {
      title: '分录金额为约整数或尾数一致',
      key: '0-0-0-4',
      // icon: ({ selected }) => (selected ? <FrownFilled /> : <FrownOutlined />),
    },
    {
      title: '自定义条件测试',
      key: '0-0-0-5',
      // icon: ({ selected }) => (selected ? <FrownFilled /> : <FrownOutlined />),
    },
    {
      title: '其他会计分录测试模型',
      key: '0-0-0-6',
      // icon: ({ selected }) => (selected ? <FrownFilled /> : <FrownOutlined />),
    },
  ];

  const treeSel1 = (selectedKeys: any[], e: any) => {
    const { node } = e;
    if (selectedKeys.length > 0) {
      setSelectedItems1(selectedKeys);
      setSelectedItems2([]);
      setTreeSelecNode((oldData: any) => {
        const newData: any = { ...oldData };
        newData.treeSelectKey = selectedKeys[0];
        newData.treeSelectName = node.title;
        return newData;
      });
    }
  };

  const treeSel2 = (selectedKeys: any[], e: any) => {
    const { node } = e;

    if (selectedKeys.length > 0) {
      setTreeSelecNode((oldData: any) => {
        const newData: any = { ...oldData };
        if (selectedKeys[0] === '0-0-0-6') {
          msgInfo(`正在打开 其他会计分录测试模型,请稍候...`);
          // UserCode={1} UserPwd={c4ca4238a0b923820dcc509a6f75849b} ProjectCode={202202-AUD-2020-001} ZcbfID={202202-AUD-2020-001}
          if (window.electronComponents) {
            const sessionParams = JSON.parse(window.sessionStorage.electronParams);
            const { sjxmbh, ZcbfID } = sessionParams.params;
            window.location.href = `cpas://OpenLocalFile/?fileName={UFCPAS4_PATH}/UFCPAS4.exe&params=UserCode={UserCode} UserPwd={UserPwd} ProjectCode=${sjxmbh} ZcbfID=${ZcbfID} Functions=FM_103`;
          } else {
            console.error('window["electronComponents"] false');
          }
        } else {
          setSelectedItems2(selectedKeys);
          setSelectedItems1([]);
          newData.treeSelectName = node.title;
          newData.treeSelectKey = selectedKeys[0];
        }
        return newData;
      });
    }
  };

  function leftContainer() {
    return (
      <div style={{ marginRight: '15px', height: '100%' }} className="AccountingEntryTestLeftCon">
        <Collapse defaultActiveKey={['1', '2']} expandIconPosition={'end'}>
          <Panel header="完整性测试" key="1">
            <Tree
              className="noneSwitcherIcon"
              // switcherIcon={false}
              blockNode
              treeData={integrityData}
              onSelect={treeSel1}
              selectedKeys={selectedItems1}
            />
          </Panel>
          <Panel header="细节测试" key="2">
            <Tree
              className="noneSwitcherIcon"
              blockNode
              treeData={detailData}
              onSelect={treeSel2}
              selectedKeys={selectedItems2}
            />
          </Panel>
        </Collapse>
      </div>
    );
  }

  // 表格结构
  function rightContainer() {
    return (
      <div style={{ marginLeft: '15px', height: '100%' }} id="entryTest-rightContainer">
        <RightTables />
      </div>
    );
  }

  return (
    <div style={{ height: '100%', minWidth: '859px', overflow: 'auto' }}>
      <TreeSelectKeyContext.Provider
        value={{ treeSelecNode, setTreeSelecNode, resizeWidthObj }}
      >
        <PageLoading
          isOpen={treeSelecNode.spinLoad}
          isMaskClose={false}
          content={'正在获取数据...'}
          width={200}
        />
        <LayoutB
          rowSize={['250px', '300px', '600px']}
          leftSlot={leftContainer()}
          rightSlot={rightContainer()}
        />
      </TreeSelectKeyContext.Provider>
    </div>
  );
};
export default AccountingEntryTest;
export { TreeSelectKeyContext }; // 必须这样写 重要！
